//undetermined :1、首次授权，未确定是否可使用此权限，此时调用对应的API时系统会弹出提示框让用户确认  
//authorized:	之前同意了授权
//denied:	之前拒绝了权限
//denied_always:	之前拒绝了权限
//none:	无此权限
//error:出错
const AndroidPermission = {
	names:{
		PUSH:'PUSH',
		LOCATION:'android.permission.ACCESS_FINE_LOCATION',
		RECORD:'android.permission.RECORD_AUDIO',
		CAMERA:'android.permission.CAMERA',
		ALBUM:'android.permission.READ_EXTERNAL_STORAGE',//scope.album
		CALL_PHONE:'android.permission.CALL_PHONE',
		CONTACT:'android.permission.READ_CONTACTS',
		CALENDAR:'android.permission.READ_CALENDAR',
		STATE:'android.permission.READ_PHONE_STATE',
		FLOAT_WINDOW:'android.permission.SYSTEM_ALERT_WINDOW',
		MEMO:'',
		INSTALL:'android.permission.INSTALL_PACKAGES',
		SHORTCUT:'com.android.launcher.permission.INSTALL_SHORTCUT',
	},
	check(permissionID) {
		let permissionIdentity = permissionID.toUpperCase();

		return new Promise((resolve, reject) => {
			console.log(permissionIdentity);
			let permissionName = this.names[permissionIdentity];
			if (!permissionName){
				return reject('none',{code: -1,message: '无须授权'});
			}
			var main = plus.android.runtimeMainActivity();
			if (permissionIdentity=='PUSH'){
				
				var pkName = main.getPackageName();
				var uid = main.getApplicationInfo().plusGetAttribute("uid");
				var NotificationManagerCompat = plus.android.importClass("androidx.core.app.NotificationManagerCompat");
				var areNotificationsEnabled = NotificationManagerCompat.from(main).areNotificationsEnabled();
				if (areNotificationsEnabled){
					return resolve('authorized');
				}else{
					return reject('denied',{code: -1,message: '未开启'});
				}

			}else {
				let permission = 'undetermined';
				var ActivityCompat = plus.android.importClass("androidx.core.app.ActivityCompat");
				if (!ActivityCompat){
					ActivityCompat = plus.android.importClass("androidx.core.content.PermissionChecker");
				}
				if (ActivityCompat){
					permission = ActivityCompat.checkSelfPermission(main,permissionName);
					switch (permission) {
						case 0:   // 允许
						  resolve('authorized');
						  break;
						case -1:    // 拒绝
						  reject('denied',{code: -1,message: '拒绝访问'});
						  break;
						default:
						  reject('error',{code: -1,message: '没有此权限'});
						  break;
					}
				}else{
					permission = plus.navigator.checkPermission(permissionIdentity);
					switch (permission) {
						case 'authorized':   // 允许
						  resolve('authorized');
						  break;
						case 'denied':    // 拒绝
						  reject('denied',{code: -1,message: '拒绝访问'});
						  break;
						case 'undetermined':    // 询问
						  resolve('undetermined');
						  break;
						case 'unknown':    // 未知
						  reject('none',{code: -1,message: '未知错误'});
						  break;
						default:
						  reject('error',{code: -1,message: '没有此权限'});
						  break;
					}
				}
				
			}

		
		});

	},
	request(permissionID) {
		var permissionIdentity = permissionID.toUpperCase();
		return new Promise((resolve, reject) => {
			let permissionName = this.names[permissionIdentity];
			if (!permissionName){
				return resolve('none');
			}
			if (permissionIdentity=='PUSH'){
				var result = 'undetermined'
				this.setting(permissionIdentity);
				resolve(result);
			}else{	
				plus.android.requestPermissions(
					[permissionName], // 理论上支持多个权限同时查询，但实际上本函数封装只处理了一个权限的情况。有需要的可自行扩展封装
					function(resultObj) {

						var result = 'undetermined';
						for (var i = 0; i < resultObj.granted.length; i++) {
							var grantedPermission = resultObj.granted[i];
							console.log('已获取的权限：' + grantedPermission);
							result = 'authorized';
							resolve(result);
						}
						for (var i = 0; i < resultObj.deniedPresent.length; i++) {
							var deniedPresentPermission = resultObj.deniedPresent[i];
							console.log('拒绝本次申请的权限：' + deniedPresentPermission);
							result = 'denied'
							reject(result,{code:i,message:'已拒绝此权限申请'});
							
						}
						for (var i = 0; i < resultObj.deniedAlways.length; i++) {
							var deniedAlwaysPermission = resultObj.deniedAlways[i];
							console.log('永久拒绝申请的权限：' + deniedAlwaysPermission);
							result = 'denied_always';
							reject(result,{code:i,message:'已永久拒绝本此权限申请'});
						}

						// 若所需权限被拒绝,则打开APP设置界面,可以在APP设置界面打开相应权限
						if (result == 'undetermined' ) {
							resolve(result);
						}
					},
					function(error) {
						console.log('申请权限错误：' + error.code + " = " + error.message);
						reject('error',{
							code: error.code,
							message: error.message
						});
					}
				);
			}
		});
	},
	setting(permissionID='') {
		var permissionIdentity = permissionID.toUpperCase();
		if (permissionIdentity=='PUSH'){
			var main = plus.android.runtimeMainActivity();
			var pkName = main.getPackageName();
			var uid = main.getApplicationInfo().plusGetAttribute("uid");
			var Intent = plus.android.importClass('android.content.Intent');
			var Build = plus.android.importClass("android.os.Build");
			//android 8.0引导  
			if (Build.VERSION.SDK_INT >= 26) {
				var intent = new Intent('android.settings.APP_NOTIFICATION_SETTINGS');
				intent.putExtra('android.provider.extra.APP_PACKAGE', pkName);
			} else if (Build.VERSION.SDK_INT >= 21) { //android 5.0-7.0  
				var intent = new Intent('android.settings.APP_NOTIFICATION_SETTINGS');
				intent.putExtra("app_package", pkName);
				intent.putExtra("app_uid", uid);
			} else { //(<21)其他--跳转到该应用管理的详情页  
				intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
				var uri = Uri.fromParts("package", mainActivity.getPackageName(), null);
				intent.setData(uri);
			}
			// 跳转到该应用的系统通知设置页  
			main.startActivity(intent);
		}else{
			var Intent = plus.android.importClass("android.content.Intent");
			var Settings = plus.android.importClass("android.provider.Settings");
			var Uri = plus.android.importClass("android.net.Uri");
			var mainActivity = plus.android.runtimeMainActivity();
			var intent = new Intent();
			intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
			var uri = Uri.fromParts("package", mainActivity.getPackageName(), null);
			intent.setData(uri);
			mainActivity.startActivity(intent);			
		}

	}
}
export default AndroidPermission;